package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class Adressbook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Addressbook
{
    private ArrayList<Person> persons;
    private static Addressbook instance;
    
    private Addressbook()
    {
        persons = new ArrayList<Person>();
    }
    
    public static Addressbook getInstance()
    {
        if(instance == null)
        {
            instance = new Addressbook();
        }
        return instance;
    }
    
    public void addPerson(Person person)
    {
        persons.add(person);
    }
    
    public Person getID(long id)
    {
        int index = 0;
        Person person = null;
        boolean found = false;
        
        while(index < persons.size() && !found)
        {
            person = persons.get(index);
            
            if(person.getID() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        if(found)
        {
            return persons.get(index);
        }
        else
        {
            return null;
        }
    }
    
    public boolean deletePerson(long id)
    {
        int index = 0;
        Person person = null;
        boolean found = false;
        
        while(index < persons.size() && !found)
        {
            person = persons.get(index);
            
            if(person.getID() == id)
            {
                persons.remove(index);
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        return found;
    }
    
    public ArrayList<Person> getArrayList()
    {
        return persons;
    }
    
    public Person getPersonIndex(int index)
    {
        return persons.get(index);
    }
}
